package com.example.order.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.example.order.Bean.Order;
import com.example.order.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
//@RequestMapping("/api/order")
public class OrderController {

    @Autowired
    OrderService orderService;

    // 创建订单
    @GetMapping("/create")
    public Order createOrder(@RequestParam("productId") Long productId,
                             @RequestParam("userId") Long userId) {
        return orderService.createOrder(productId,userId);
    }

    // 秒杀订单
    @GetMapping("/secKill")
    @SentinelResource(value = "secKill-order",fallback = "secKillFallBack")
    public Order secKill(@RequestParam("productId") Long productId,
                             @RequestParam("userId") Long userId) {
        return orderService.createOrder(productId,userId);
    }

    // 秒杀订单兜底回调
    public Order secKillFallBack(Long productId, Long userId, Throwable e) {
        System.out.println("secKillFallBack.....");
        Order order = new Order();
        order.setId(0L);
        order.setUserId(userId);
        order.setAddress("异常信息:" + e.getClass());
        return order;
    }

    // 从数据库中读取订单
    @GetMapping("/readDb")
    public String readDb() {
        log.info("readDb...");
        return "readDb success...";
    }
}
